package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Actor;
import com.wanmait.movie.entity.Admin;
import com.wanmait.movie.entity.Brand;
import com.wanmait.movie.entity.Cinema;
import com.wanmait.movie.service.BrandService;
import com.wanmait.movie.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("manage/brandType")
public class ManageBrandTypeController {
        @Resource
        private BrandService brandService;
        @GetMapping("list")
        public AjaxResult list( Integer pageNum){
            if (pageNum == null || pageNum == 0){
                pageNum = 1;
            }
            return AjaxResult.success("查询成功",brandService.getBrandList(pageNum,5)).put("total",brandService.getBrandList(pageNum,5).getTotal());
        }
        @GetMapping("delete")
        public AjaxResult delete(Integer id){
            if(!brandService.removeById(id)){
                return AjaxResult.error(506,"删除错误");
            }
            return AjaxResult.success(brandService.removeById(id));
        }
        @PostMapping("saveOrUpdate")
        public AjaxResult saveOrUpdate(@RequestBody Brand brand, HttpServletRequest request){
            if (!brandService.saveOrUpdate(brand)){
                return AjaxResult.error(505,"添加或修改错误");
            }
            brand.setUpdateTime(new Date());
            // 新增人
            Admin admin = (Admin) request.getAttribute("admin");
            brand.setUpdateAdminId(admin.getId());
            return AjaxResult.success(brandService.saveOrUpdate(brand));
        }
    @PostMapping("add")
    public AjaxResult addCinema(@RequestBody Brand brand, HttpServletRequest request) {
        // 新增时间
        brand.setInsertTime(new Date());
        // 新增人
        Admin admin = (Admin) request.getAttribute("admin");
        brand.setInsertAdminId(admin.getId());
        return AjaxResult.success(brandService.save(brand));
    }

}
